package monitor

import (
	consts2 "MoSkeleton/config/consts"
	"MoSkeleton/framework/core/consts"
	"MoSkeleton/framework/core/dto/request"
	"MoSkeleton/framework/utils"
	"MoSkeleton/models/monitor"
	"github.com/gin-gonic/gin"
	"time"
)

type OperLog4GinReq struct {
	c            *gin.Context
	Title        string
	BusinessType consts2.BusinessType
	OperatorType consts2.OperatorType
	//是否保存请求的参数
	SaveRequestData bool
	//是否保存响应的参数
	SaveResponseData bool
	//排除指定的请求参数
	ExcludeParamNames []string
	operBeginTime     time.Time
}

func NewOperLog4GinReq(c *gin.Context) *OperLog4GinReq {
	oper := &OperLog4GinReq{}
	oper.c = c
	oper.BusinessType = consts2.BusinessType_OTHER
	oper.OperatorType = consts2.OperatorType_MANAGE
	oper.SaveRequestData = true
	oper.SaveResponseData = true
	oper.ExcludeParamNames = []string{}
	oper.operBeginTime = time.Now()
	c.Set(consts.GinContext_OperLog_key, oper)
	return oper
}

func (o *OperLog4GinReq) CopyTo(operLog *monitor.SysOperLog) error {
	err := utils.CopyStruct(operLog, o)
	if err != nil {
		return err
	}
	operLog.CostTime = int64(time.Now().Sub(o.operBeginTime) / time.Millisecond)
	operLog.BusinessType = int32(o.BusinessType)
	operLog.OperatorType = int32(o.OperatorType)
	return nil
}

type SysOperLogPageReq struct {
	request.PageInfo
	request.BaseRequest
	OperIp        string  `json:"operIp" form:"operIp"`
	Title         string  `json:"title" form:"title"`
	BusinessType  *int32  `json:"businessType" form:"businessType"`
	BusinessTypes []int32 `json:"businessTypes" form:"businessTypes"`
	Status        *int    `json:"status" form:"status"`
	OperName      string  `json:"operName" form:"operName"`
}
